package edu.zut.cs.network.sqlproject.teach.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import edu.zut.cs.network.sqlproject.base.service.BaseManager;
import edu.zut.cs.network.sqlproject.teach.dao.StudentDao;
import edu.zut.cs.network.sqlproject.teach.dao.impl.StudentDaoImpl;
import edu.zut.cs.network.sqlproject.teach.entity.Student;
import edu.zut.cs.network.sqlproject.teach.service.StudentManager;

public class StudentManagerImpl implements StudentManager{
	protected Logger logger = LogManager.getLogger(this.getClass().getName());
	List<Student> studentList;
	Map<String, Student> studentMap;
	Map<Long, Student> studentIdMap;
	StudentDao studentDao = new StudentDaoImpl();
	public void load(String data_file) throws FileNotFoundException, ClassNotFoundException, IOException {
		// TODO Auto-generated method stub
		this.studentList = new ArrayList<Student>();
		this.studentMap = new HashMap<String, Student>();
		this.studentIdMap = new HashMap<Long, Student>();

		File file = new File(data_file);
		InputStream input = new FileInputStream(file);
		XSSFWorkbook wb = null;
		try {
			wb = new XSSFWorkbook(input);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		XSSFSheet sheet = wb.getSheetAt(1);
		for (int i = 1; i <= 29; i++) {
			Row row = sheet.getRow(i);
			Student s = new Student();
			s.setId(Long.valueOf(1000 + i));
			for (int j = 0; j < row.getLastCellNum(); j++) {
				Cell cell = row.getCell(j);
				if (cell != null) {
					cell.setCellType(CellType.STRING);
					String value = String.valueOf(row.getCell(j).getStringCellValue());
					if (j == 0)
						s.setCode(value);
					if (j == 1)
						s.setFullName(value);
					if (j == 2)
						s.setClassAndGrades(value);;
				}
			}
			logger.info(" student_" + i + " is :" + s);
			new StudentDaoImpl().create(s);
			this.studentList.add(s);
			this.studentMap.put(s.getFullName(), s);
			this.studentIdMap.put(s.getId(), s);
		}
	}
	public void create(Student student) {
		// TODO Auto-generated method stub
		studentDao.create(student);
	}
	public void remove(String code) {
		// TODO Auto-generated method stub
		studentDao.remove(code);
	}
	public void update() throws SQLException {
		// TODO Auto-generated method stub
		studentDao.update();
	}
	public Student findByName(String name) {
		// TODO Auto-generated method stub
		return studentDao.findByName(name);
	}
	public Student findById(Long id) {
		// TODO Auto-generated method stub
		return studentDao.findById(id);
	}
	

}
